package com.jiangnan.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jiangnan.mapper.StudentMapper;
import com.jiangnan.pojo.PageResult;
import com.jiangnan.pojo.Student;
import com.jiangnan.pojo.StudentQueryParam;
import com.jiangnan.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentMapper studentMapper;

    @Override
    public PageResult<Student> page(StudentQueryParam studentQueryParam) {
        // 1. 设置分页参数
        PageHelper.startPage(studentQueryParam.getPage(), studentQueryParam.getPageSize());
        // 2. 执行查询语句
        List<Student> studentList = studentMapper.getPage(studentQueryParam);
        // 3. 解析查询结果，封装为 PageResult 对象并返回
        Page<Student> p = (Page<Student>) studentList;
        return new PageResult<>(p.getTotal(), p.getResult());
    }

    @Override
    public void deleteByIds(List<Integer> ids) {
        studentMapper.deleteBatch(ids);
    }

    @Override
    public void save(Student student) {
        // 1. 设置创建时间
        student.setCreateTime(LocalDateTime.now());
        student.setUpdateTime(LocalDateTime.now());
        // 2. 保存学生信息
        studentMapper.insert(student);
    }

    @Override
    public Student getById(Integer id) {
        Student student = studentMapper.getById(id);
        return student;
    }

    @Override
    public void update(Student student) {
        // 1. 设置更新时间
        student.setUpdateTime(LocalDateTime.now());
        // 2. 更新学生信息
        studentMapper.update(student);
    }

    @Override
    public void updateViolation(Integer id, Integer score) {
        // 1. 设置更新时间 放在SQL里了，因为没有传入student

        // 2. 更新学生信息
        studentMapper.updateViolation(id, score);
    }
}
